/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: performance_index.cpp
 */

#include <iostream>
#include <string>
#include <exception>
#include <cmath>

#include "performance_index.hpp"

//-----------------------------------------------------------------------------
// Function implementations
namespace middleware {
namespace calculus {
double Log(double base, double arg) {
	return ((std::log(arg)) / (std::log(base)));
}
double Sqrt(double arg) {
	return std::sqrt(arg);
}
double Exp(double base, double arg) {
	return std::pow(base, arg);
}
PIType CalculatePI(const PerformanceEvalParams& pep) {
	double N = -1, S = -1, C = -1; /* Parts */
	double PI = -1; /* Final result */
	N = (double)(kPerformanceEvalBeta * pep.eval_bandwidth);
	N = (double)(N / (double)((kPerformanceEvalDelta * (pep.eval_hops + 1))));
	N = (double)(Sqrt(N));
	S = (double)(pep.eval_cpu_mhz * pep.eval_cpu_num);
	S = (double)(S / ((double)(pep.eval_load + 1)));
	C = Log(kPerformanceEvalG, pep.eval_core_num);
	C = (double)(C + 1);
	C = (double)((double)1 + Exp(kPerformanceEvalH, -(pep.eval_ram_mb - 512)));
	PI = (double)(N * S * C);
	return (PIType)PI;
}
}
}
